﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Property;

namespace Cofe.Core.Utils
{
    /// <summary>
    /// Raise event when write.
    /// </summary>
    public class NonClosableMemoryStream : HookableMemoryStream
    {
        public NonClosableMemoryStream()
        {
            
        }

#if !NETFX_CORE 
        public override void Close()
        {
            OnClosing(this, EventArgs.Empty);
            //base.Close();
            IsModified = false;
            OnClosed(this, EventArgs.Empty);
        }
#endif

    }


    //public class VirtualFileStream : MemoryStream
    //{
    //    public VirtualFileStream(IPropertyHost propertyHost, FileMode mode, FileAccess access)
    //        : base()
    //    {

    //        switch (access)
    //        {
    //            case FileAccess.Write :
    //                break;
    //            case FileAccess.Read :
    //            case FileAccess.ReadWrite : 
    //                using (var streamContainer =  propertyHost.Behaviors.Invoke<StreamContainer>(CofeStreamProperties.OpenReadAsync))
    //                    StreamUtils.CopyStream(streamContainer.Stream, this, false, true, false);                    
    //                break;                    
    //        }

    //        _file = propertyHost;
    //        _access = access;
    //        _mode = mode;
    //        _isModified = false;
    //    }



    //    #region Methods

    //     public override void Write(byte[] buffer, int offset, int count)
    //    {
    //        base.Write(buffer, offset, count);
    //        _isModified = true;
    //    }

    //    public override void WriteByte(byte value)
    //    {
    //        base.WriteByte(value);
    //        _isModified = true;
    //    }

    //    public override void EndWrite(IAsyncResult asyncResult)
    //    {
    //        base.EndWrite(asyncResult);
    //        _isModified = true;
    //    }


    //    public virtual void CallUpdateProc()
    //    {
    //        if (_isModified)
    //        {
    //            using (var streamContainer = _file.Behaviors.Invoke<StreamContainer>(CofeStreamProperties.OpenWriteAsync))
    //                StreamUtils.CopyStream(streamContainer.Stream, this, false, false, false);

    //            _isModified = false;
    //        }
    //    }

    //    protected override void Dispose(bool disposing)
    //    {

    //        base.Dispose(disposing);
    //    }

    //    public override void Close()
    //    {
    //        base.Close();
    //        CallUpdateProc();
    //    }

    //    #endregion

    //    #region Data

    //    private FileAccess _access;
    //    private FileMode _mode;
    //    private IPropertyHost _file;
    //    private bool _isModified;

    //    #endregion

    //}
}
